package com.dinggou.shopjb.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.dinggou.shopjb.R;
import com.dinggou.shopjb.activitys.AddAddressActivity;
import com.dinggou.shopjb.activitys.AddGoodsOrderActivity;
import com.dinggou.shopjb.activitys.AddressListActivity;
import com.dinggou.shopjb.activitys.BankListActivity;
import com.dinggou.shopjb.activitys.BeforeRechargeActivity;
import com.dinggou.shopjb.activitys.BindCardCertificateActivity;
import com.dinggou.shopjb.activitys.BindCardFaceVerifyActivity;
import com.dinggou.shopjb.activitys.CalendarDetailActivity;
import com.dinggou.shopjb.activitys.CertificateIdCardActivity;
import com.dinggou.shopjb.activitys.FindBackPwdActivity;
import com.dinggou.shopjb.activitys.FollowListActivity;
import com.dinggou.shopjb.activitys.FollowOrderCenterActivity;
import com.dinggou.shopjb.activitys.FollowOrderUserActivity;
import com.dinggou.shopjb.activitys.GoodsImageDetailActivity;
import com.dinggou.shopjb.activitys.HomeAddGoodsOrderActivity;
import com.dinggou.shopjb.activitys.InvestSchoolActivity;
import com.dinggou.shopjb.activitys.LoginRegisterActivity;
import com.dinggou.shopjb.activitys.MainActivity;
//import com.qifang.shengniuorder.activitys.MarketNewActivity;
import com.dinggou.shopjb.activitys.MarketNewActivity;
import com.dinggou.shopjb.activitys.MessageActivity;
import com.dinggou.shopjb.activitys.MyFollowOrderActivity;
import com.dinggou.shopjb.activitys.OrderDetailActivity;
import com.dinggou.shopjb.activitys.RechargeActivity;
import com.dinggou.shopjb.activitys.RechargeHasCardActivity;
import com.dinggou.shopjb.activitys.RedBagRainActivity;
import com.dinggou.shopjb.activitys.SetPayPwdActivity;
import com.dinggou.shopjb.activitys.SettingActivity;
import com.dinggou.shopjb.activitys.TakeMoneyActivity;
import com.dinggou.shopjb.activitys.TicketActivity;
import com.dinggou.shopjb.activitys.TurnCardActivity;
import com.dinggou.shopjb.activitys.UserCenterActivity;
import com.dinggou.shopjb.activitys.WebViewActivity;
import com.dinggou.shopjb.activitys.WelfareActivity;
import com.dinggou.shopjb.application.MyApplication;
import com.dinggou.shopjb.base.BaseActivity;
import com.dinggou.shopjb.bean.AddressBean;
import com.dinggou.shopjb.bean.CalendarDetail;
import com.dinggou.shopjb.bean.OrderInfoBean;
import com.dinggou.shopjb.bean.ShopGoodsListBean;
import com.dinggou.shopjb.bean.TxLiveTokenBean;
import com.dinggou.shopjb.bean.UserBankCardBean;
import com.dinggou.shopjb.core.AppAction;
import com.dinggou.shopjb.net.ActionCallbackListener;
import com.dinggou.shopjb.view.CertificateModule;
import com.zc.mychart.model.GoodsAllBean;

import java.util.List;

public class RouteUtil {
    public static final String BUNDLE_LOGIN_REGISTER_TYPE = "login_register_type";//登录注册type
    public static final String BUNDLE_MAIN_PAGE = "main_page";//首页页面
    public static final String BUNDLE_HOME_GOODS_MODEL = "home_goods_model";
    public static final String BUNDLE_BEAN_MODEL = "bean";
    public static final String BUNDLE_PRO_CODE = "pro_code";
    public static final String PAGE_WITHDRAW = "page_withdraw";//提现
    public static final String PAGE_RECHARGE = "page_recharge";//充值
    public static final String BUNDLE_USER_BANK_MODEL = "user_bank_model";//用户银行卡信息
    public static final String BUNDLE_RECHARGE_MONEY = "recharge_money";//充值金额
    public static final String BUNDLE_RECHARGE_CODE = "recharge_code";//充值code
    public static final String BUNDLE_ORDER_MODEL = "order_info_model";//订单详情
    public static final String BUNDLE_IS_HISTORY = "is_history";//是否历史订单详情
    public static final String BUNDLE_BANK_MODEL = "bank_model";//银行信息
    public static final String TXLIVE_TOKEN_MODEL = "tx_live_model";//腾讯人脸信息
    public static final String BUNDLE_ADDRESS = "address_model";//地址信息
    public static final String BUNDLE_LIVE_RESULT = "live_result";//人脸结果
    public static final String BUNDLE_LIVE_IS_SUCCESS = "live_result_success";//人脸是否成功
    public static final String BUNDLE_USER_ID = "user_id";//用户id
    public static final String BUNDLE_CALENDAR_MODEL = "calendar_model";//财经详情
    public static final int REQUEST_ADDRESS_LIST = 1003;//地址回传
    public static final int REQUEST_BANK_LIST = 1001;//银行卡回传
    public static final int REQUEST_LIVE_REQUEST = 1010;//人脸识别回传

    public static void toCalendarDetailActivity(BaseActivity mActivity, CalendarDetail bean) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(BUNDLE_CALENDAR_MODEL, bean);
        gotoActivity(mActivity, CalendarDetailActivity.class, bundle);
    }

    /**
     * 去跟单广场页面
     *
     * @param mActivity
     */
    public static void toFollowOrderCenterActivity(BaseActivity mActivity) {
        gotoActivity(mActivity, FollowOrderCenterActivity.class, null);
    }

    /**
     * 去添加地址页面
     *
     * @param mActivity
     * @param bean
     */
    public static void toAddAddressActivity(BaseActivity mActivity, AddressBean bean) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(BUNDLE_ADDRESS, bean);
        gotoActivity(mActivity, AddAddressActivity.class, bundle);
    }

    /**
     * 去人脸识别
     *
     * @param mActivity
     * @param name
     * @param cert
     * @param requestCode
     */
    public static void toFaceVerifyActivity(BaseActivity mActivity, String name, String cert, TxLiveTokenBean txLiveTokenBean, int requestCode) {
        Bundle bundle = new Bundle();
        bundle.putString("name", name);
        bundle.putString("cert", cert);
        bundle.putSerializable(TXLIVE_TOKEN_MODEL, txLiveTokenBean);

        gotoActivityForResult(mActivity, BindCardFaceVerifyActivity.class, bundle, requestCode);
    }

    /**
     * 去新手指南
     *
     * @param mActivity
     */
    public static void toInvestSchoolNewActivity(BaseActivity mActivity) {
        gotoActivity(mActivity, InvestSchoolActivity.class, null);
    }

    /**
     * 跳转到地址管理
     *
     * @param mActivity
     * @param requestCode
     */
    public static void toAddressListActivity(BaseActivity mActivity, int requestCode) {
        gotoActivityForResult(mActivity, AddressListActivity.class, null, requestCode);
    }

    /**
     * 跳转到首页
     *
     * @param mActivity
     * @param type      到具体的fragment
     */
    public static void toMainActivity(BaseActivity mActivity, int type) {
        Intent intent = new Intent(mActivity, MainActivity.class);
        //将其他activity移除
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(RouteUtil.BUNDLE_MAIN_PAGE, type);
        mActivity.startActivity(intent);
    }

    /**
     * 跳转到订单详情
     *
     * @param mActivity
     * @param bean
     */
    public static void toOrderDetailActivity(BaseActivity mActivity, OrderInfoBean bean, boolean isHistory) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(BUNDLE_ORDER_MODEL, bean);
        bundle.putBoolean(BUNDLE_IS_HISTORY, isHistory);
        gotoActivity(mActivity, OrderDetailActivity.class, bundle);
    }


    /**
     * 跳转到用户中心
     *
     * @param baseActivity
     */
    public static void toUserCenterActivity(BaseActivity baseActivity) {
        gotoActivity(baseActivity, UserCenterActivity.class, null);
    }

    /**
     * 跳转到福利中心
     *
     * @param mActivity
     */
    public static void toWelfareActivity(BaseActivity mActivity) {
        gotoActivity(mActivity, WelfareActivity.class, null);
    }

    /**
     * 跳转到消息中心
     *
     * @param mActivity
     */
    public static void toMessageActivity(BaseActivity mActivity) {
        gotoActivity(mActivity, MessageActivity.class, null);
    }


    /**
     * 银联支付跳转逻辑
     *
     * @param mActivity
     * @param page
     * @param money
     * @param code
     */
    public static void goMoneyModuleActivity(final BaseActivity mActivity,
                                             final String page, final double money, final String code) {
        AppAction mAppAction = MyApplication.getApplication().getAppAction();
        mActivity.loading();
        mAppAction.getCardList(code, new ActionCallbackListener<UserBankCardBean>() {
            @Override
            public void onSuccess(UserBankCardBean data) {
                mActivity.cancelLoading();
                if (PAGE_WITHDRAW.equals(page)) {
                    goWithDrawActivity(mActivity);
                    if (mActivity instanceof CertificateIdCardActivity) {
                        backActivity(mActivity);
                    }
                } else if (PAGE_RECHARGE.equals(page)) {
                    if (null == data || TextUtils.isEmpty(data.card_no)) {
                        goRechargeActivity(mActivity, money, code);
                    } else {
                        goRechargeHasCardActivity(mActivity, data, money, code);
                    }
                }
            }

            @Override
            public void onFailure(String errorCode, String message) {
                mActivity.cancelLoading();
            }
        });
    }

    /**
     * 无银行卡绑定充值
     *
     * @param mActivity
     * @param money
     * @param code
     */
    public static void goRechargeActivity(BaseActivity mActivity, double money, String code) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(BUNDLE_RECHARGE_MONEY, money);
        bundle.putSerializable(BUNDLE_RECHARGE_CODE, code);
        gotoActivity(mActivity, RechargeActivity.class, bundle);
    }

    /**
     * 有银行卡绑定充值
     *
     * @param mActivity
     * @param cardBean
     * @param money
     * @param code
     */
    public static void goRechargeHasCardActivity(BaseActivity mActivity, UserBankCardBean cardBean, double money, String code) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(BUNDLE_USER_BANK_MODEL, cardBean);
        bundle.putSerializable(BUNDLE_RECHARGE_MONEY, money);
        bundle.putSerializable(BUNDLE_RECHARGE_CODE, code);
        gotoActivity(mActivity, RechargeHasCardActivity.class, bundle);
    }

    /**
     * 去选择银行
     *
     * @param mActivity
     * @param requestCode
     */
    public static void goBankListActivity(BaseActivity mActivity, int requestCode, String code) {
        Bundle bundle = new Bundle();
        bundle.putString(BUNDLE_RECHARGE_CODE, code);
        gotoActivityForResult(mActivity, BankListActivity.class, bundle, requestCode);
    }

    /**
     * 去提现页面
     *
     * @param mActivity
     */
    public static void goWithDrawActivity(BaseActivity mActivity) {
        gotoActivity(mActivity, TakeMoneyActivity.class, null);
    }

    /**
     * 红包雨
     *
     * @param mActivity
     */
    public static void toRedRainActivity(BaseActivity mActivity) {
        gotoActivity(mActivity, RedBagRainActivity.class, null);
    }

    /**
     * 天天翻牌
     *
     * @param mActivity
     */
    public static void toTurnCardActivity(BaseActivity mActivity) {
        gotoActivity(mActivity, TurnCardActivity.class, null);
    }

    /**
     * 实名认证页面
     *
     * @param baseActivity
     */
    public static void toCertificateIdCardActivity(BaseActivity baseActivity) {
        gotoActivity(baseActivity, CertificateIdCardActivity.class, null);
    }

    /**
     * 绑卡页面
     *
     * @param mActivity
     */
    public static void toBindCardCertificateActivity(BaseActivity mActivity) {
        gotoActivity(mActivity, BindCardCertificateActivity.class, null);
    }

    /**
     * 跳转登录注册
     *
     * @param mActivity
     * @param type      0注册 1登录
     */
    public static void toLoginRegisterActivity(BaseActivity mActivity, int type) {
        Bundle bundle = new Bundle();
        bundle.putInt(BUNDLE_LOGIN_REGISTER_TYPE, type);
        Intent intent = new Intent(mActivity, LoginRegisterActivity.class);
        intent.putExtras(bundle);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        mActivity.startActivity(intent);
        mActivity.overridePendingTransition(R.anim.push_right_in, R.anim.not_exit_push_left_out);
    }

    /**
     * 去webview页面
     *
     * @param mActivity
     * @param url       地址
     */
    public static void toWebViewActivity(BaseActivity mActivity, String url) {
        Bundle bundle = new Bundle();
        bundle.putString("url", url);
        gotoActivity(mActivity, WebViewActivity.class, bundle);
    }

    /**
     * 跳转到找回密码
     *
     * @param baseActivity
     */
    public static void toFindBackPwdActivity(BaseActivity baseActivity) {
        gotoActivity(baseActivity, FindBackPwdActivity.class, null);
    }

    /**
     * 跳转到找回密码
     *
     * @param baseActivity
     */
    public static void tosetPayPwdActivity(BaseActivity baseActivity) {
        gotoActivity(baseActivity, SetPayPwdActivity.class, null);
    }

    /**
     * 零售商品详情
     *
     * @param mActivity
     * @param bean
     */
    public static void toGoodsImageDetailActivity(BaseActivity mActivity, ShopGoodsListBean bean) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(BUNDLE_HOME_GOODS_MODEL, bean);
        gotoActivity(mActivity, GoodsImageDetailActivity.class, bundle);
    }

    /**
     * 零售商品下单
     *
     * @param mActivity
     * @param bean
     */
    public static void toHomeAddGoodsOrderActivity(BaseActivity mActivity, ShopGoodsListBean bean) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(BUNDLE_HOME_GOODS_MODEL, bean);
        gotoActivity(mActivity, HomeAddGoodsOrderActivity.class, bundle);
    }

    /**
     * 跳转交易市场
     *
     * @param mActivity
     * @param pro_code  默认展示得合约code
     */
    public static void toMarketActivity(BaseActivity mActivity, String pro_code) {
        Bundle bundle = new Bundle();

        if (TextUtils.isEmpty(pro_code)) {
            List<GoodsAllBean> allList = MyApplication.getApplication().getAllList();
            if (null != allList && allList.size() > 0) {
                bundle.putString(BUNDLE_PRO_CODE, allList.get(0).pro_code);
            } else {
                ToastUtil.showToast(mActivity, "暂未获取商品信息");
            }
        } else {
            bundle.putString(BUNDLE_PRO_CODE, pro_code);
        }

        gotoActivity(mActivity, MarketNewActivity.class, bundle);
    }

    /**
     * 去设置页面
     *
     * @param mActivity
     */
    public static void toSettingActivity(BaseActivity mActivity) {
        gotoActivity(mActivity, SettingActivity.class, null);
    }

    /**
     * 去充值页面
     *
     * @param mActivity
     */
    public static void toRechargeActivity(BaseActivity mActivity) {
        if (isUserValidLogin(mActivity)) {
            gotoActivity(mActivity, BeforeRechargeActivity.class, null);
        }
    }

    /**
     * 提货页面
     *
     * @param mActivity
     * @param bean
     */
    public static void toAddGoodsOrderActivity(BaseActivity mActivity, OrderInfoBean bean) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(BUNDLE_ORDER_MODEL, bean);
        gotoActivity(mActivity, AddGoodsOrderActivity.class, bundle);
    }

    /**
     * 高手详情
     *
     * @param mActivity
     * @param userId
     */
    public static void toFollowOrderUserActivity(BaseActivity mActivity, String userId) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(BUNDLE_USER_ID, userId);
        gotoActivity(mActivity, FollowOrderUserActivity.class, bundle);
    }

    /**
     * 我得关注
     *
     * @param mActivity
     */
    public static void toFollowListActivity(BaseActivity mActivity) {
        gotoActivity(mActivity, FollowListActivity.class, null);
    }

    /**
     * 我的跟单列表
     *
     * @param mActivity
     */
    public static void toMyFollowOrderActivity(BaseActivity mActivity) {
        gotoActivity(mActivity, MyFollowOrderActivity.class, null);
    }

    /**
     * 去代金券页面
     *
     * @param mActivity
     */
    public static void toTicketActivity(BaseActivity mActivity) {
        gotoActivity(mActivity, TicketActivity.class, null);
    }


    /**
     * 跳转到某个Activity
     */
    public static void gotoActivity(Context mContext, Class<?> toActivityClass, Bundle bundle) {
        Intent intent = new Intent(mContext, toActivityClass);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        mContext.startActivity(intent);

        ((Activity) mContext).overridePendingTransition(R.anim.push_right_in, R.anim.not_exit_push_left_out);
    }

    public static void gotoActivityForResult(Context mContext, Class<?> toActivityClass, Bundle bundle, int requestCode) {
        Intent intent = new Intent(mContext, toActivityClass);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        ((Activity) mContext).startActivityForResult(intent, requestCode);
        ((Activity) mContext).overridePendingTransition(R.anim.push_right_in, R.anim.not_exit_push_left_out);
    }

    /**
     * 未登录或登录过期状态需要做出提示时调用此方法
     */
    private static boolean isUserValidLogin(Context mContext) {
        if (DataUtils.getUserId(mContext).equals("0") || !DataUtils.isLogin(mContext)) {
            onLogoutStatus(mContext);
            return false;
        }
        return true;
    }

    /**
     * 根据状态跳转页面
     */
    private static void onLogoutStatus(Context mContext) {
        if (DataUtils.isRegister(mContext)) {
            toLoginRegisterActivity((BaseActivity) mContext, LoginRegisterActivity.TYPE_LOGIN);
        } else {
            toLoginRegisterActivity((BaseActivity) mContext, LoginRegisterActivity.TYPE_REGISTER);
        }
    }

    /**
     * 关闭某个页面
     */
    public static void backActivity(Context mContext) {
        ((Activity) mContext).overridePendingTransition(R.anim.not_exit_push_left_in, R.anim.push_right_out);
        ((Activity) mContext).finish();
    }

    public static boolean routePageByUrl(BaseActivity mActivity, String link) {
        if (TextUtils.isEmpty(link)) {
            return false;
        }
        if (link.equals("app://toQuestion")) {
//            if (mActivity.isUserValidLogin())
//                toNewQuestionActivity(mActivity);
        } else if (link.equals("app://toGuess")) {
//            if (mActivity.isUserValidLogin())
//                toGuessActivity(mActivity);
        } else if (link.equals("app://toSaving")) {
            if (mActivity.isUserValidLogin())
                toRechargeActivity(mActivity);
        } else if (link.equals("app://toSavingCG")) {
            if (mActivity.isUserValidLogin())
                toRechargeActivity(mActivity);
        } else if (link.equals("app://toTasks")) {
            if (mActivity.isUserValidLogin())
                toMainActivity(mActivity, MainActivity.TAB_WELFATE);
        } else if (link.equals("app://toRedboxNew")) {
//            if (mActivity.isUserValidLogin())
//                toRedBoxRainActivity(mActivity);
        } else if (link.equals("app://toWithdraw")) {
            if (mActivity.isUserValidLogin())
                if (CertificateModule.getInstance(mActivity).isCertificate(RouteUtil.PAGE_WITHDRAW)) {
                    RouteUtil.goMoneyModuleActivity(mActivity, RouteUtil.PAGE_WITHDRAW, 0, "");
                }
        } else if (link.equals("app://toOrder")) {
            toMarketActivity(mActivity, "");
        } else if (link.equals("app://toSns")) {
        } else if (link.equals("app://toUser")) {
            toMainActivity(mActivity, MainActivity.TAB_MY);
        } else if (link.equals("app://toWelfare")) {
            toWelfareActivity(mActivity);
        } else if (link.equals("app://toFollowOrder")) {
            toFollowOrderCenterActivity(mActivity);
        } else if (link.equals("app://toTicket")) {
            gotoActivity(mActivity, TicketActivity.class, null);
        } else if (link.equals("app://toShare")) {
//            toShareActivity(mActivity);
        } else {
            if (link.contains("http")) {
                toWebViewActivity(mActivity, link);
            }

        }
        return true;
    }

}
